package com.tarena.lbs.basic.web.service.impl;

import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.mapper.UserTagsMapper;
import com.tarena.lbs.basic.web.repository.UserGroupRepository;
import com.tarena.lbs.basic.web.repository.UserRepository;
import com.tarena.lbs.basic.web.service.IUserGroupService;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.dos.UserTagsDO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.query.MarketingSelectedQuery;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

@Service
public class UserGroupServiceImpl implements IUserGroupService {

    @Autowired
    private UserGroupRepository userGroupRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserTagsMapper userTagsMapper;

    @Override
    public boolean verificationByUserGroupIDS(List<Integer> ids) {
        List<UserGroupDO> userGroupDOS = userGroupRepository.selectByIds(ids);
        if (ids.size() == userGroupDOS.size()) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isUserInTargetGroup(int userId,String userGroupIds) {
        List<Integer> tagList = userRepository.selectTagIdByUserById(userId);
        System.out.println(tagList);
        Assert.notNull(tagList,"用户标签为null");
        Assert.notNull(userGroupIds,"目标人群参数异常，请检查参数");
        String[] userGroupIdArray = userGroupIds.split(",");
        System.out.println(Arrays.toString(userGroupIdArray));
        List<Integer> UserGroupIdList = new ArrayList<>();
        for (String userGroupId : userGroupIdArray) {
            UserGroupIdList.add(Integer.valueOf(userGroupId));
        }
        List<UserGroupDO> userGroupDOS = userGroupRepository.selectByIds(UserGroupIdList);
        HashSet<Integer> userGroupTagList = new HashSet<>();
        for (UserGroupDO userGroupDO : userGroupDOS) {
            String[] split = userGroupDO.getTagIds().split(",");
            for (String s : split) {
                userGroupTagList.add(Integer.valueOf(s));
            }
        }
        return userGroupTagList.containsAll(tagList);

    }

    @Override
    public List<Integer> getUserByTargetGroup(List<Integer> userGroupIds) {
        if(CollectionUtils.isEmpty(userGroupIds)) {
            return null;
        }
        Set<Integer> set = new HashSet<>();
        List<UserGroupDO> userGroupDOS = userGroupRepository.selectByIds(userGroupIds);
        for (UserGroupDO userGroupDO : userGroupDOS) {
            String[] split = userGroupDO.getTagIds().split(",");
            for (String temp : split) {
                set.add(Integer.valueOf(temp));
            }
        }
        List<UserTagsDO> userTagsList = userTagsMapper.selectByTagIdList(new ArrayList<>(set));
        if (CollectionUtils.isEmpty(userTagsList)) {
            return null;
        }
        List<Integer> userIdList = userTagsList.stream().map(e -> e.getUserId()).collect(Collectors.toList());
        HashSet<Integer> userIdSet = new HashSet<>(userIdList);
        return new ArrayList<>(userIdSet);
    }

    @Override public BasePageList<UserGroupBO> userList(UserGroupQuery query) {
        return userGroupRepository.list(query);
    }

    @Override public UserGroupBO getDetail(Integer id) {
        return userGroupRepository.getDetail(id);
    }

    @Override
    public void insert(UserGroupParam param) {
        userGroupRepository.insert(param);
    }

    @Override public BasePageList<UserGroupBO> marketingUserGroupList(MarketingSelectedQuery query) {
        return userGroupRepository.marketingUserGroupList(query);
    }
}
